<template>
  <div class="logincontainer">
    <el-form
      :model="loginForm"
      ref="loginForm"
      :inline="false"
      size="normal"
      class="loginForm"
      :rules="rules"
    >
      <el-form-item size="normal">
        <div class="loginTitle">系统登录</div>
      </el-form-item>
      <el-form-item label="" prop="username">
        <el-input
          v-model="loginForm.username"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item label="" prop="password">
        <el-input
          type="password"
          v-model="loginForm.password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-row :gutter="20">
          <el-col :span="16" :offset="0">
            <el-input
              v-model="loginForm.code"
              placeholder="请输入验证码"
            ></el-input>
          </el-col>
          <el-col :span="8" :offset="0">
            <img class="codeimg" :src="imgSrc" @click="changeImage" />
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-button
              type="primary"
              size="default"
              class="btn"
              @click="handlerLogin('loginForm')"
              >登录</el-button
            >
          </el-col>
          <el-col :span="12">
            <el-button size="default" class="btn">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getImageApi } from '@/api/user'
export default {
  name: 'logon',
  data () {
    return {
      imgSrc: '',
      loginForm: {
        username: 'admin',
        password: '1234',
        code: '1'
      },
      rules: {
        username: [
          {
            required: true,
            trigger: 'change',
            message: '请输入用户名'
          }
        ],
        password: [
          {
            required: true,
            trigger: 'change',
            message: '请输入密码'
          }
        ],
        code: [
          {
            required: true,
            trigger: 'change',
            message: '请输入验证码'
          }
        ]
      }
    }
  },
  created () {
    this.getImage()
  },
  methods: {
    changeImage () {
      this.getImage()
    },
    // 获取验证码
    async getImage () {
      this.imgSrc = await getImageApi()
    },
    handlerLogin (name) {
      this.$refs[name].validate(valid => {
        // 这两种验证的方法都可以传递数据，就是表单的名称
        // 都可以达到需要的效果
        // this.$refs.loginForm.validate(valid => {
        // 表单验证通过
        if (valid) {
          this.loading = true
          // 调用登录,调用user模块的login方法,并传入一个参数this.loginForm
          // 因为是异步方法,只会在action中,所以只能使用dispatch的方法来调用
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || '/',
                query: this.otherQuery
              })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.logincontainer {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .loginForm {
    height: 350px;
    width: 400px;
    border-radius: 10px;
    box-shadow: 0 0 25px #cac6c6;
    padding: 20px 35px;
    .loginTitle {
      display: flex;
      justify-content: center;
      align-items: center;
      font-weight: 600;
      font-size: 24px;
    }
    .codeimg {
      width: 110px;
      height: 40px;
      cursor: pointer;
    }
    .btn {
      width: 100%;
    }
  }
}
</style>
